<?php
namespace app\common\model;
use think\Model;
use think\Validate;
use app\common\controller\Tools;
class Enterprise extends Model
{
	protected $table = 'enterprise';
	protected $pk = 'enterprise_id';
	// 验证规则
	protected $rule = [
		// ['enterprise_license_img|营业执照扫描件', ['file','fileExt:jpg,png,gif', 'fileSize:2*1024*1024']],
		['enterprise_name|企业名称', ['require', 'max:60']],
		['enterprise_reg_amount|注册资金', ['require', 'number', '>:0']],
		['enterprise_reg_number|营业执照注册号', ['require']],
		['enterprise_license_reg_time|营业执照开始时间', ['require', 'date']],
		['enterprise_license_end_time|营业执照到期时间', ['require', 'date']],
		['enterprise_license_location|营业执照所在地', ['require']],
		['enterprise_legal_person|法人姓名', ['require']],
		['enterprise_contacts_person|企业联系人姓名', ['require']],
		['enterprise_contacts_tel|企业联系人手机', ['require']],
		['enterprise_is_certificates|是否多证合一', ['require']],
		['enterprise_organization_code_reg_time|组织结构代码注册时间', ['require']],
		['enterprise_organization_code_end_time|组织结构代码到期时间', ['require']],
		['enterprise_idc_up_img|法人身份证正面', ['require']],
		['enterprise_idc_down_img|法人身份证背面', ['require']],
	]; 
	
	// 验证场景
	protected $scene = [
        'insert' => ['enterprise_name', 'enterprise_reg_amount', 'enterprise_reg_number', 'enterprise_legal_person', 'enterprise_contacts_person', 'enterprise_contacts_person'],
        'update' => ['enterprise_name', 'enterprise_reg_amount', 'enterprise_reg_number', 'enterprise_legal_person', 'enterprise_contacts_person', 'enterprise_contacts_person'],
    ];

    // 验证器
	public function intValidate()
	{
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1=>$v1)
            $validate->scene($k1, $v1);
        return $validate;
    }

   	public function upload($column, $who, $thumb=[])
   	{
   		$tool = new Tools();
		$result = $tool->upload_img($column, $who, $thumb);
		$filename = $result['images'][1];
		$pos = strrpos($filename, '/');
		$filename = substr($filename, $pos+1); 
		// halt($filename);
		return $filename;
	}
				
				
				
    protected static function init()
	{
		Enterprise::beforeInsert(function ($enterprise) 
		{
			// 商户id
			$enterprise['enterprise_merchant_id'] = session('merchant')['merchant_id'];
			// 审核状态
			$enterprise['enterprise_verify_status'] = 1;

			// 营业执照所在地
			$province = input('param.province');
			$city = input('param.city');
			$enterprise['enterprise_license_location'] = $province . ',' . $city;
            $enterprise_license_end_time_1 = input('param.enterprise_license_end_time_1', '');

            // 营业执照有效期
			$reg_time = $enterprise['enterprise_license_reg_time']; 
			$end_time = $enterprise['enterprise_license_end_time'] ; 
			$enterprise['enterprise_license_reg_time'] = strtotime($reg_time);
            if ($enterprise_license_end_time_1 == 'on') {
                $enterprise['enterprise_license_end_time'] = 1;
            } else {
                $enterprise['enterprise_license_end_time'] = strtotime($end_time);
            }

			// 组织机构与代码有效期
			// 1:组织机构与代码有效期不用写入数据库，2：则写入数据库
			$code_reg_time = input('param.enterprise_organization_code_reg_time');
			$code_end_time = input('param.enterprise_organization_code_end_time');
			$enterprise_is_certificates = (int)input('param.enterprise_is_certificates');
			if($enterprise_is_certificates == 1) {
				$enterprise['enterprise_organization_code_reg_time'] = 0;
				$enterprise['enterprise_organization_code_end_time'] = 0;
			}
			if($enterprise_is_certificates == 2) {
				$enterprise['enterprise_organization_code_reg_time'] = strtotime($code_reg_time);
				$enterprise['enterprise_organization_code_end_time'] = strtotime($code_end_time);
			}
		});
	}

	public static function getImgName($imgName)
	{
		$pos = strrpos($imgName, '/');
		$imgName = substr($imgName, $pos);
		return $imgName;
	}
	public static function getEnterpriseAndShop()
	{
		$merchant_id = session('merchant')['merchant_id'];
		$data['enterprise'] = Enterprise::where('enterprise_merchant_id', $merchant_id)->find();
		$data['storeInfo'] = model('store')->where('store_merchant_id', $merchant_id)->find();
		return $data;
	}
	public static function getEnterprise($id)
	{
		$enterprise = Enterprise::where('enterprise_id', $id)->find();
		return $enterprise;
	}  

   //获取enterprise
	public  static function getEnterpriseId($merchant_id)
	{
		return  Enterprise::where('enterprise_merchant_id', $merchant_id)->value('enterprise_id');
	}

	// 相关属性获取器
	// 执照注册时间
	public function getEnterpriseLicenseRegTimeAttr($value)
	{
		 
		//$value = date('Y-m-d', $value);
		return $value;
	}

	 public static function ConvertDate($value)
	 {
		 $value = date('Y-m-d', $value);
		return $value;
	 }
	// 执照到期时间
	public function getEnterpriseLicenseEndTimeAttr($value)
	{
		 
		//$value = date('Y-m-d', $value);
		return $value;
	}

	// 营业执照所在地
	public function getEnterpriseLicenseLocationAttr($value)
	{
		$value = explode(',', $value);
		return $value;
	}

	// 营业执照图片路径
	// public function getEnterpriseLicenseImgAttr($value)
	// {
	// 	$merchant_img_path = config('IMAGE_CONFIG');
	// 	$value = $merchant_img_path['certificat_img_path']. $value;
	// 	return $value;
	// }

	// 身份证正面
	// public function getEnterpriseIdcUpImgAttr($value)
	// {
	// 	$merchant_img_path = config('IMAGE_CONFIG');
	// 	$value = $merchant_img_path['idcard_img_path']. $value;
	// 	return $value;
	// }

	// 身份证背面
	// public function getEnterpriseIdcDownImgAttr($value)
	// {
	// 	$merchant_img_path = config('IMAGE_CONFIG');
	// 	$value = $merchant_img_path['idcard_img_path']. $value;
	// 	return $value;
	// }

	public function getEnterpriseVerifyStatusAttr($value)
	{
		$status = [0=>'正在审核', 1=>'审核通过', 2=>'审核未通过'];
		return $status[$value];
	}

	public static function showlist($pagesize)
	{
		$where = [];
		$where['enterprise_is_delete'] = ['eq', 0];
		// 关键字搜索(企业名称，营业执照注册号)
 		$keyword = input('param.keyword');
		if($keyword)
			$where['a.enterprise_name|a.enterprise_reg_number|c.store_name'] = ['like', "%$keyword%"] ;
    	$data = Enterprise::field('a.enterprise_id,a.enterprise_name,a.enterprise_reg_number,a.enterprise_license_reg_time,a.enterprise_license_end_time,a.enterprise_verify_status,c.store_id,c.store_name')->alias('a')
    	->join('merchant b', 'a.enterprise_merchant_id=b.merchant_id', 'left')
    	->join('store c', 'b.merchant_id=c.store_merchant_id', 'left')
    	->where($where)
    	->order('a.enterprise_id', 'desc')
    	->paginate($pagesize);
        		
     	return $data;
	}

	// public static function getEnterprise($enterprise_id)
	// {
	// 	$data = Enterprise::find($enterprise_id);
	// 	return $data;
	// }


	public static function changeDel($enterprise_id)
	{
       	$result = Enterprise::where('enterprise_id', $enterprise_id)->setField('enterprise_is_delete', 1);
       	if(!$result)
       		return false;

       	return true;
    }


    
}

